import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gameplay_flutter/lib_baseview/root_page.dart';
import 'package:gameplay_flutter/m_me/constants/me_assets_constants.dart';

class CustomTabBarIndicator extends Decoration {

   const CustomTabBarIndicator();

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _CustomTabBarIndicatorPainter();
  }
}

class _CustomTabBarIndicatorPainter extends BoxPainter {

  _CustomTabBarIndicatorPainter();

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final Paint paint = Paint();

    // 定义固定宽高
    final double fixedWidth = 13.2.w; // 固定宽度
    final double fixedHeight = 5.2.h; // 固定高度

    // 计算绘制区域，居中对齐
    final Rect rect = Rect.fromLTWH(
      offset.dx + (configuration.size!.width - fixedWidth) / 2, // 水平居中
      offset.dy + configuration.size!.height - fixedHeight,     // 垂直底部对齐
      fixedWidth,
      fixedHeight,
    );

    // 使用 AssetImage 作为 ImageProvider
    const ImageProvider imageProvider = AssetImage(MeAssetsConstants.iconIndicator);

    // 解析图片流
    final ImageStream stream = imageProvider.resolve(configuration);
    stream.addListener(ImageStreamListener((ImageInfo info, bool _) {
      // 计算图片的缩放比例，确保图片按比例填充固定宽高
      final double scaleX = fixedWidth / info.image.width.toDouble();
      final double scaleY = fixedHeight / info.image.height.toDouble();

      // 绘制图片到指定区域
      canvas.save();
      canvas.translate(rect.left, rect.top); // 移动到绘制区域
      canvas.scale(scaleX, scaleY); // 缩放图片
      canvas.drawImage(info.image, Offset.zero, paint);
      canvas.restore();
    }));
  }
}

